###
#  File paths
###
secure<-[PATH]
  secure_nsc<-[PATH]
  secure_cb<-[PATH]
  secure_ucb<-[PATH]
  secure_ucd<-[PATH]
  secure_ucr<-[PATH]
  secure_ucsc<-[PATH]
  secure_ucsb<-[PATH]
secure_derived<-[PATH]


library(doSNOW) #Version 1.0.18
library(readxl) #Version 1.3.1
library(sas7bdat) #Version 0.5
library(haven) #Version 2.3.1
library(plyr) #Version 1.8.6
library(dplyr) #Version 1.0.5
library(reshape2) #Version 1.4.4
library(readr) #Version 1.4.0
library(spatstat) #Version 1.64-1
library(ipumsr) #Version 0.4.4
library(nnet) #Version 7.3-14
library(sp) #Version 1.4-2
library(maptools) #Version 1.0-1
library(rgeos) #Version 0.5-3
library(multiwayvcov) #Version 1.2.3
library(rdd) #Version 0.57
library(foreign) #Version 0.8-80
library(lfe) #Version 2.8-5
library(rdrobust) #Version 0.99.8
library(ggplot2) #Version 3.3.3
library(HonestDiD) #Version 0.1.0

NA_to_F<-function(x){
  x[is.na(x)]<-FALSE
  return(x)
}
NA_to_T<-function(x){
  x[is.na(x)]<-TRUE
  return(x)
}

allduplicated<-function(x){
  return(duplicated(x)|duplicated(x,fromLast = T))
}

titleCase <- function(x) {
  conj<-c("and","or") #Can add to this, but MUST keep "or"; otherwise read as "Oregon" for City list
  x<-gsub("([,.-])([A-z])","\\1 \\2",x)
  y<-sapply(x,function(y){
    y<-unlist(strsplit(y," "))
    y[y%in%conj]<-paste0(" ",y[y%in%conj])
    temp<-paste0(toupper(substring(y, 1,1)), tolower(substring(y, 2)),collapse=" ")
    temp[temp=="NANA"]<-NA
    temp<-gsub("\\s+"," ",temp)
    return(temp)
  })
  return(gsub("[-]\\s","-",y))
}

Mode <- function(x,return.all=FALSE,na.rm=FALSE) {
  ux <- unique(x)
  if(na.rm){ ; ux<-ux[!is.na(ux)] ; if(length(ux)==0) return(NA) ; }
  t<-tabulate(match(x, ux))
  ux<-ux[t==max(t)]
  if(return.all) return(ux)
  return(ux[1])
}

norm<-function(x){
  return((x-mean(x,na.rm=T))/sd(x,na.rm=T))
}

inrange<-function(x,a=0,b=0){
  return(x>=a&x<=b)
}

winsor<-function(x,n){
  x[x>quantile(x,(1-n),na.rm=T)]<-quantile(x,(1-n),na.rm=T)
  x[x<quantile(x,n,na.rm=T)]<-quantile(x,n,na.rm=T)
  return(x)
}

addzeros<-function(t,n){
  for(i in 1:(n-1)) t[nchar(t)<n]<-paste0("0",t[nchar(t)<n])
  return(t)
}

simpleCap <- function(x) {
  s <- tolower(strsplit(x, " ")[[1]])
  paste(toupper(substring(s, 1,1)), substring(s, 2),
        sep="", collapse=" ")
}

t_col <- function(color, percent = 50, name = NULL) {
  ## Transparent colors
  ## Mark Gardener 2015
  ## www.dataanalytics.org.uk
  #      color = color name
  #    percent = % transparency
  #       name = an optional name for the color
  
  ## Get RGB values for named color
  rgb.val <- col2rgb(color)
  
  ## Make new color using input color as base and alpha set by transparency
  t.col <- rgb(rgb.val[1], rgb.val[2], rgb.val[3],
               max = 255,
               alpha = (100 - percent) * 255 / 100,
               names = name)
  
  ## Save the color
  invisible(t.col)
}

ex<-function(x,i,par=F,brk=F,p=-1,comma=F,dagger=0.1,justnum=F){
  miss<-is.na(x) ; x[miss]<-0 ; if(length(p)==length(miss)) p[miss]<- -1
  if(par) hi<-paste0('="(',sprintf(paste0("%.",i,"f"), round(x,i)),')"')
  else if(brk) hi<-paste0('="[',sprintf(paste0("%.",i,"f"), round(x,i)),']"')
  else hi<-paste0('="',sprintf(paste0("%.",i,"f"), round(x,i)),'"')
  if(p[1]>=0){
    check<-inrange(p,0.05000000001,dagger) ; hi[check]<-gsub('(\\]|[)])?("$)',"†\\1\\2",hi[check])
    check<-inrange(p,0.01000000001,0.05) ; hi[check]<-gsub('(\\]|[)])?("$)',"*\\1\\2",hi[check])
    check<-p<0.01 ; hi[check]<-gsub('(\\]|[)])?("$)',"**\\1\\2",hi[check])
  }
  if(comma){
    for(zzz in 1:3) hi<-gsub('([0-9]{3}|(["(-]|\\[)[0-9]{1,2})([0-9]{3})([",.)†*]|\\])',"\\1,\\3\\4",hi)
  }
  if(justnum) hi<-gsub('["=]',"",hi)
  hi[miss]<-'=""'
  return(hi)
} 










genmat<-function(R,r,c,depvar,data,IV=F,asian=F,hispanic=F,black=F,multieth=F,singlediff=F,multieth_URM=F,Use9697=F,numdec=2){
  if(Use9697) data$Prop209<-data$YEARAPAY>1995
  data$URM<-data$URM==1 #Make sure it's a logical
  if(asian){
    data$URM[!data$CETHNICA=="P"]<-NA
    data$URM[data$CETHNICA%in%c("D","E","G","H","L","N","V")]<-T
  } 
  if(hispanic) data$URM[data$URM==1&!data$CETHNICA%in%c("C","J")]<-NA
  if(black) data$URM[data$URM==1&!data$CETHNICA%in%c("B")]<-NA
  if(multieth_URM){
    data$URM<-data$Black ; data$Asian<-data$Hispanic
    data$URM[data$CETHNICA%in%c("A")]<-NA
    multieth<-T
  }
  if(length(table(data[,depvar]))==2|grepl("ACS|HASGRAD|STEM_(No|UC)?NSC|NoNSC|NSC_Major|GradRate",depvar)) data$DepVar<-data[,depvar]*100 else data$DepVar<-data[,depvar]
  for(v in c("URM","Asian")){
    data[,paste0(v,"Pre209")]<-data[,v]==1&data$Prop209==0 #&data$YEARAPAY!=1997
    data[,paste0(v,"Post209")]<-data[,v]==1&data$Prop209==1
  }
  if(singlediff) data$Prop209<-data$Prop209*(data$URM==0)
  
  #data$URMjustPre209<-data$URM==1&data$YEARAPAY==1997 #Don't need to do this; no big change in admissions this year, though there is a change in applications... And doesn't matter much for results. Can come back to this at some point.
  #data$Time<-0 ; data$Time[data$YEARAPAY==1997]<-1 ; data$Time[data$YEARAPAY>1997]<-2
  t<-summary(felm(DepVar~URM+Prop209+URMPost209 + SATIM_nom+SATIV_nom+hsgpa+SATIIW_nom+SATII_M_nom+factor(SATII_M2_Indic_nom)+SATII_Other_nom+SATIM_m+SATIV_m+SATIIW_m+SATII_M_m+SATII_Other_m | factor(CPREVSCH)+factor(SATII_1_nom),data=data),robust=T) #Prop209*income_parent_zeros+Prop209*income_Missing+Female+HSCRSE_TYP_ACADHONRS_12 #Cluster
  if(multieth) tm<-summary(felm(DepVar~URM+Asian+Prop209+URMPost209+AsianPost209 + SATIM_nom+SATIV_nom+hsgpa+SATIIW_nom+SATII_M_nom+factor(SATII_M2_Indic_nom)+SATII_Other_nom+SATIM_m+SATIV_m+SATIIW_m+SATII_M_m+SATII_Other_m | factor(CPREVSCH)+factor(SATII_1_nom),data=data),robust=T) #Cluster_Eth
  t1<-t$coefficients[c("URMTRUE","URMPost209TRUE"),]
  if(singlediff) t1<-t$coefficients[c("Prop209","URMPost209TRUE"),]
  
  R[r,c]<-ex(t1[1,1],numdec,comma=T)
  R[r+1,c]<-ex(t1[1,2],numdec,par=T,comma=T)
  R[r+3,c]<-ex(t1[2,1],numdec,comma=T)
  R[r+4,c]<-ex(t1[2,2],numdec,par=T,comma=T)
  #R[r+6,c]<-ex(t1[3,1],numdec)
  #R[r+7,c]<-ex(t1[3,2],numdec,par=T)
  R[r+6,c]<-ex(mean(data$DepVar[!is.na(data$URM)&!is.na(data$Prop209)],na.rm=T),numdec,comma=T)  #ex(t$r2,2)
  R[r+7,c]<-ex(t$N,0,comma=T)
  
  if(multieth){
    tm1<-tm$coefficients[c("URMTRUE","URMPost209TRUE","AsianTRUE","AsianPost209TRUE"),]
    if(singlediff) tm1<-tm$coefficients[c("Prop209","URMPost209TRUE","AsianTRUE","AsianPost209TRUE"),]
    names(R)[(c+1):(c+2)]<-paste0(c("URM_","Asian_"),depvar)
    if(multieth_URM) names(R)[(c+1):(c+2)]<-paste0(c("Black_","Hispanic_"),depvar)  
    for(i in 1:2){
      R[r,c+i]<-ex(tm1[1+(2*(i-1)),1],numdec,comma=T)
      R[r+1,c+i]<-ex(tm1[1+(2*(i-1)),2],numdec,par=T,comma=T)
      R[r+3,c+i]<-ex(tm1[2+(2*(i-1)),1],numdec,comma=T)
      R[r+4,c+i]<-ex(tm1[2+(2*(i-1)),2],numdec,par=T,comma=T)
      #R[r+6,c]<-ex(t1[3,1],numdec)
      #R[r+7,c]<-ex(t1[3,2],numdec,par=T)
      R[r+6,c+i]<-ex(mean(data$DepVar[!is.na(data$URM)&!is.na(data$Prop209)],na.rm=T),numdec,comma=T)
      R[r+7,c+i]<-ex(tm$N,0,comma=T)
    }
  }
  
  
  if(IV){
    r<-r+10
    data$URMProp209<-data$URM&data$Prop209
    t<-summary(felm(DepVar~URM+Prop209 + SATIM_nom+SATIV_nom+hsgpa+SATIIW_nom+SATII_M_nom+factor(SATII_M2_Indic_nom)+SATII_Other_nom+SATIM_m+SATIV_m+SATIIW_m+SATII_M_m+SATII_Other_m | factor(CPREVSCH)+factor(SATII_1_nom) | (UCAdm~URMProp209),data=data),robust=T) #+Female+HSCRSE_TYP_ACADHONRS_12
    t1<-t$coefficients[row.names(t$coefficients)%in%c("URMTRUE","`UCAdm(fit)`"),]
    R[r,c]<-ex(t1[2,1],2)
    R[r+1,c]<-ex(t1[2,2],2,par=T)
    R[r+4,c]<-ex(t$r2,3)
    R[r+5,c]<-ex(t$N,0,comma=T)
    
    r<-r+8
    t<-summary(felm(DepVar~URM+Prop209 + SATIM_nom+SATIV_nom+hsgpa+SATIIW_nom+SATII_M_nom+factor(SATII_M2_Indic_nom)+SATII_Other_nom+SATIM_m+SATIV_m+SATIIW_m+SATII_M_m+SATII_Other_m | factor(CPREVSCH)+factor(SATII_1_nom) | (UCAdm~URMProp209),data=data[NA_to_F(data$Pros_Major_STEM>0),]),robust=T) #+Female+HSCRSE_TYP_ACADHONRS_12
    t1<-t$coefficients[row.names(t$coefficients)%in%c("URMTRUE","`UCAdm(fit)`"),]
    R[r,c]<-ex(t1[2,1],2)
    R[r+1,c]<-ex(t1[2,2],2,par=T)
    
    R[r+4,c]<-ex(t$r2,3)
    R[r+5,c]<-ex(t$N,0,comma=T)
  }
  return(R)
}
